package com.acc.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.acc.entity.User;
import com.acc.service.HelloService;
import com.acc.utils.Md5Utils;
/**
 * 
 * @author 杨天然
 *
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = -334493491471408658L;


	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
	    HelloService helloService = new HelloService();

		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		
		//取出表单中的参数
		String userName = req.getParameter("username");
		String pwd = req.getParameter("password");
		// 进行密码加密
		String password = Md5Utils.md5(pwd); 
		// 判断是否可以自动登录
		String check = req.getParameter("check");
		// 取出登录信息对应的用户名以及密码的用户
		User user = helloService.getOne(userName, password); 
		if (user != null) {
			// 获取session
			HttpSession session = req.getSession();
			//往session中设置总条目数(count)以及当前页码数(pageNum)
			// 判断验证码是否输入正确
			if (!check.equals(session.getAttribute("code"))) {
				resp.sendRedirect("/TicketManager/index.jsp");
			} else {
				
				session.setAttribute("user", user);
				req.getRequestDispatcher("/reception.jsp").forward(req, resp);

			}
		} else {
			req.getRequestDispatcher("/index.jsp").forward(req, resp);

		}
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

}
